package show.me.the.money.service.dao;


import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import show.me.the.money.config.schema.Schema;
import show.me.the.money.config.schema.SchemaController;
import show.me.the.money.dao.QueryResult;
import show.me.the.money.dao.SimpleDAO;
import show.me.the.money.dao.exception.DataAccessException;
import show.me.the.money.service.core.Service;
import show.me.the.money.util.context.Context;




@SuppressWarnings("unchecked")
public class SimpleQuery extends DBService {
	private static final int DEFAULT_PAGESIZE = 25;
	private static Log logger = LogFactory.getLog(SimpleQuery.class);
	public void execute(HashMap<String,Object> req, HashMap<String,Object> res, Context ctx){
		int code = 200;
		String msg = "Success";
		
		String schemaId = (String)req.get("schema");
		if(StringUtils.isEmpty(schemaId)){
			code = 401;
			msg = "SchemaIdMissed";
			res.put(Service.RES_CODE,code);
			res.put(Service.RES_MESSAGE, msg);
			return;
		}
		
		Schema sc = SchemaController.instance().getSchema(schemaId);
		if(sc == null){
			code = 402;
			msg = "NoSuchSchema";
			res.put(Service.RES_CODE,code);
			res.put(Service.RES_MESSAGE, msg);
			return;
		}
		
		List queryCnd = null;
		if(req.containsKey("cnd")){
			queryCnd = (List)req.get("cnd");
		}
		
		String queryCndsType = null;
		if(req.containsKey("queryCndsType")){
			queryCndsType = (String)req.get("queryCndsType");
		}
		
		String sortInfo = null;
		if(req.containsKey("sortInfo")){
			sortInfo = (String)req.get("sortInfo");
		}
		
		int pageSize= DEFAULT_PAGESIZE;
		if(req.containsKey("pageSize")){
			pageSize = (Integer)req.get("pageSize");
		}
		
		int pageNo = 0;
		if(req.containsKey("pageNo")){
			pageNo = (Integer)req.get("pageNo");
		}
		
		SimpleDAO dao = null;
		try{
			dao = new SimpleDAO(sc,ctx);
	
			QueryResult qr = dao.find(queryCnd, pageNo, pageSize, queryCndsType, sortInfo);
			
			res.put("pageSize", pageSize);
			res.put("pageNo", pageNo);
			res.put("totalCount",qr.getTotalCount());
			res.put("body", qr.getRecords());
			doAfterExec(req,res,ctx);
			long costTime = qr.getCostTime();
			ctx.put("$P_SQL_COST_TIME", costTime);
		}
		catch(DataAccessException e){
			code = 501;
			msg = "DataAccessException:" + e.getMessage();
			res.put(Service.RES_CODE,code);
			res.put(Service.RES_MESSAGE,msg);
		}
		catch(Exception e){
			code = 500;
			msg = "UnknowException:" + e.getMessage();
			res.put(Service.RES_CODE,code);
			res.put(Service.RES_MESSAGE,msg);
		}
		finally{
			if(dao != null){
				try {
					dao.destroy();
				} 
				catch (DataAccessException e) {
					logger.error("DaoDestroyFailed:" + e.getMessage());
				}
			}
		}
	}
	
	public void doAfterExec(HashMap<String,Object> req, HashMap<String,Object> res, Context ctx) {
		
	}	
	
	public void doBeforeExec(HashMap<String,Object> req, HashMap<String,Object> res, Context ctx){
		
	}
	
}